home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / 3D & Offscreen for MacApp 3 / 3D & Offscreen Sample.sea / 3D & Offscreen Sample / TVirtualSphere3DTracker.cp < prev    next >
Text File  |  1993-05-10  |  6KB  |  186 lines

  1. /*************************************************************************
  2.  
  3.         File: TVirtualSphere3DTracker.h
  4.  
  5.         C O P Y R I G H T    N O T I C E
  6.  
  7.          Copyright ⌐ 1989, 1990, 1991, 1992 Siemens Gammasonics, Inc.
  8.         All Rights Reserved.
  9.         No portions of this source code or the resulting compiled
  10.         program may be used without express written consent and liscensing
  11.         by Siemens Gammasonics, Inc.
  12.  
  13.  
  14.         D E S C R I P T I O N
  15.  
  16.  
  17.         Classes Defined Here:
  18.  
  19.              Ñ
  20.  
  21.  
  22.      Change History
  23.  
  24.         Rev 1    Wed, Apr 14, 1993 @ 6:16 PM        Hanig
  25.             Creation
  26.  
  27.  *************************************************************************/
  28. /*************************************************************************/
  29. /*                            Include Files                                 */
  30. /*************************************************************************/
  31. #ifndef        __TVirtualSphere3DTracker__
  32. #include    "TVirtualSphere3DTracker.h"
  33. #endif
  34. #ifndef        __MATH__
  35. #include    <Math.h>
  36. #endif
  37.  
  38. /*************************************************************************/
  39. /*                                Constants                                     */
  40. /*************************************************************************/
  41.  
  42. /*************************************************************************/
  43. TVirtualSphere3DTracker::TVirtualSphere3DTracker( void )
  44. {
  45.  
  46. }
  47.  
  48. /*************************************************************************/
  49. #pragma segment MADoCommand
  50. pascal TTracker* TVirtualSphere3DTracker::TrackMouse(TrackPhase aTrackPhase,
  51.                                         VPoint& anchorPoint,
  52.                                         VPoint& /*previousPoint*/,
  53.                                         VPoint& nextPoint,
  54.                                         Boolean mouseDidMove)
  55. {
  56.  
  57.     switch (aTrackPhase)
  58.     {
  59.         case trackBegin:
  60.             break;
  61.  
  62.         case trackContinue:
  63.             if (mouseDidMove)
  64.             {
  65.                 long        dx, dy;
  66.                 VPoint        sphereCenter; 
  67.                 double        sphereRadius; 
  68.                 CPoint3D    axisOfRotation;
  69.                 double        angleOfRotation;
  70.                 CRect        drawArea;
  71.  
  72.                 /* Figure out where to place the Virtual Sphere cue circle.
  73.                  * In this sample app., the cue is always centered on the window with a fixed size.
  74.                  * In an general app., you will need to determine the location and size of cue
  75.                  * (in screen coordinates) to surround the object */
  76.                 fView->GetQDExtent (drawArea);
  77.                 sphereCenter.h = drawArea.right;
  78.                 if ( drawArea.right > drawArea.bottom )
  79.                     sphereCenter.h = drawArea.bottom;
  80.                 sphereCenter.v = sphereCenter.h/2;
  81.                 sphereCenter.h = sphereCenter.v;
  82.                 sphereRadius   = (sphereCenter.h* kSphereRadius);
  83.  
  84.  
  85.                 dx = nextPoint.h - anchorPoint.h;
  86.                 dy = nextPoint.v - anchorPoint.v;
  87.                 if (dx != 0 || dy != 0) 
  88.                 {
  89.                     /* Determine the amount of rotation from the mouse movement */
  90.                     VirtualSphere (nextPoint, anchorPoint, sphereCenter, sphereRadius, &axisOfRotation, &angleOfRotation);
  91.                     /* Compute the corresponding rotation matrix */
  92.                     
  93.                     ((T3DObjectView*)fView)->AdjustforRotation( &axisOfRotation, angleOfRotation );
  94.  
  95.                     VRect theArea;
  96.                     fView->GetExtent(theArea);
  97.                     fView->HandleDraw(theArea);
  98.                     
  99.                     anchorPoint = nextPoint;    // remember the current mouse location for the next iteration
  100.                 }
  101.             }
  102.             break;
  103.  
  104.         case trackEnd:
  105.             break;
  106.     }                                            // switch 
  107.     return this;                                // keep tracking with me 
  108. }
  109.  
  110.  
  111. /*************************************************************************/
  112. #pragma segment ThreeDSeg
  113. pascal void TVirtualSphere3DTracker::VirtualSphere (VPoint p,  VPoint q,
  114.                 VPoint cueCenter,  long cueRadius,  CPoint3D *axisOfRotation,  
  115.                 double  *angleOfRotation)
  116. {
  117.     CPoint3D        pp, qq;    
  118.     double            axisLength;
  119.     
  120.     // Project mouse points to 3D points on the +z hemisphere of a unit sphere.
  121.     PointOnUnitSphere (p, cueCenter, cueRadius, &pp);
  122.     PointOnUnitSphere (q, cueCenter, cueRadius, &qq);
  123.  
  124.     // Consider the 2 projected points as vectors from the center of the unit sphere.
  125.     // Compute the axis of rotation by cross product of these vectors.
  126.     CrossProduct3D (&qq, &pp, axisOfRotation);
  127.     
  128.     // The angle of rotation is determined from the length of the cross product.
  129.     axisLength = (sqrt (axisOfRotation->x * axisOfRotation->x + axisOfRotation->y * axisOfRotation->y + axisOfRotation->z * axisOfRotation->z));
  130.     *angleOfRotation = asin (axisLength);
  131.  
  132.     // The axis of rotation must be a unit vector, so normalize it.
  133.     if (*angleOfRotation > 0) 
  134.     {
  135.         axisOfRotation->x /= axisLength;
  136.         axisOfRotation->y /= axisLength;
  137.         axisOfRotation->z /= axisLength;
  138.     }
  139. }
  140.  
  141.  
  142. /*************************************************************************/
  143. #pragma segment ThreeDSeg
  144. pascal void TVirtualSphere3DTracker::PointOnUnitSphere (VPoint p,  VPoint cueCenter,
  145.                                 long cueRadius,  CPoint3D *v)
  146. {
  147.     double    length;
  148.     double    lengthSqared;
  149.     
  150.     /* Turn the mouse points into vectors relative to the center of the circle
  151.      * and normalize them.  Note we need to flip the y value since the 3D coordinate
  152.      * has positive y going up. */
  153.     v->x = (double)  (p.h - cueCenter.h) / (double) cueRadius;
  154.     v->y = (double) -(p.v - cueCenter.v) / (double) cueRadius;
  155.  
  156.     lengthSqared = v->x*v->x + v->y*v->y;
  157.     
  158.     /* Project the point onto the sphere, assuming orthographic projection.
  159.      * Points beyond the virtual sphere are normalized onto 
  160.      * edge of the sphere (where z = 0). */
  161.     if (lengthSqared < 1.0)
  162.         v->z = sqrt (1.0 - lengthSqared);
  163.     else 
  164.     {
  165.         length = sqrt (lengthSqared);
  166.         v->x /= length;
  167.         v->y /= length;
  168.         v->z = 0.0;
  169.     }
  170. }
  171.  
  172.  
  173. /*************************************************************************/
  174. #pragma segment ThreeDSeg
  175. pascal void TVirtualSphere3DTracker::CrossProduct3D (const CPoint3D *a,  
  176.                                 const CPoint3D *b, CPoint3D *aCrossB)
  177. {
  178.     aCrossB->x = a->y * b->z - a->z * b->y;
  179.     aCrossB->y = a->z * b->x - a->x * b->z;
  180.     aCrossB->z = a->x * b->y - a->y * b->x;
  181. }
  182.  
  183.  
  184. /*************************************************************************/
  185. /*************************************************************************/
  186.